package utility;

import org.apache.commons.codec.digest.DigestUtils;

public abstract class Security 
{
    /**
     * Author Sonny Sandberg
     * Encrypts pass to a 40 digit string
     * Requires org.apache.commons.codec.digest.DigestUtils
     * @param password
     * @return String
     */
    public static String encryptString(String password)
    {
        String encryptedPassword = DigestUtils.md5Hex(password);
        encryptedPassword = DigestUtils.sha1Hex(encryptedPassword);

        char[] passChar = encryptedPassword.toCharArray();
        char[][] pass = new char[4][10];
        
        // Break string into four pieces and reverse each part
        byte q = 0;
        for (byte i = 0; i < 4; i++)
        {
            for (byte x = 9; x >= 0; x--)
            {
                pass[i][x] = passChar[q];
                q++;
            }
        }
        
        // Build new password string
        String newPass = "";
        for (byte i = 0; i < 4; i++)
        {
            for (byte z = 0; z < 10; z++)
            {
                newPass += pass[i][z];
            }
        }
        
        return DigestUtils.sha1Hex(newPass);
    }
    
    /**
     * Author Sonny Sandberg
     * Used to create a string of the length specified. Random azAZ09 chars
     * @param int length
     * @return String
     */
    public static String createRandomString(int length)
    {
        String securitykey = "";
        String allChars = "abcdefghikjlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            
        for (int i = 0; i < length; i++) 
        {
            int rand = (int) (Math.random() * allChars.length());
            securitykey += allChars.charAt(rand);
        }
        return securitykey;
    }
}